<script setup lang="ts">
import { ref, reactive, watch, onMounted, onUnmounted, nextTick } from "vue";
import ReTable from "@/components/ReTable/index.vue";
import { useSettingStoreHook } from "@/store/modules/settings";
import ReNewDialog from "@/components/ReNewDialog/index.vue";
import ReUpload from "@/components/ReUpload/index.vue";
import ReImageDialog from "@/components/ReImageDialog/index.vue";
import {
  ElMessage,
  ElMessageBox,
  type FormInstance,
  type FormRules
} from "element-plus";
import type { Action } from "element-plus";
defineOptions({
  name: "shopTypeList"
});
interface ValueType {
  val: {
    buttonType: string;
  };
  row: object;
}
interface shopTypeFormType {
  title: string;
  image: string;
  status: string;
  sort: string;
}
const pureSetting = useSettingStoreHook();
const dialogViewVisible = ref(false);
const dialogTitle = ref("");
const ruleFormRef = ref<FormInstance>();
const showViewer = ref(false);
const imageUrl = ref("");
watch(
  () => pureSetting.url,
  (newValue, oldValue) => {
    if (newValue) {
      imageUrl.value = newValue;
      showViewer.value = true;
    }
  },
  {
    immediate: true,
    deep: true
  }
);
// 关闭查看器
const closeViewer = () => {
  showViewer.value = false;
  pureSetting.setUrl("");
};
const deleteModal = () => {
  ElMessageBox.confirm("确定要删除选中的商品分类吗?", "提示", {
    distinguishCancelAndClose: true,
    confirmButtonText: "确定",
    cancelButtonText: "取消"
  })
    .then(() => {
      ElMessage({
        type: "success",
        message: "删除成功"
      });
    })
    .catch((action: Action) => {
      ElMessage({ type: "info", message: "取消删除" });
    });
};
watch(
  () => pureSetting.buttonInfo,
  (newValue, oldValue) => {
    if (Object.keys(newValue).length !== 0) {
      let value = newValue as ValueType;
      switch (value.val.buttonType) {
        case "delete":
          deleteModal();
          break;
        case "edit":
          console.log("编辑按钮", newValue);
          dialogTitle.value = "修改商品分类";
          dialogViewVisible.value = true;
          break;
      }
    }
  },
  {
    immediate: true,
    deep: true
  }
);
const shopTypeForm = reactive<shopTypeFormType>({
  title: "",
  image: "",
  status: "1",
  sort: ""
} as shopTypeFormType);
const rules = reactive<FormRules>({
  title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  image: [{ required: true, message: "请上传图片", trigger: "blur" }],
  status: [{ required: true, message: "请选择状态", trigger: "blur" }],
  sort: [{ required: true, message: "请输入排序", trigger: "blur" }]
});
const dialogImageSelectVisible = ref(false);
const imageType = ref("");
const handleGetCreate = () => {
  dialogTitle.value = "添加商品分类";
  dialogViewVisible.value = true;
};
const handleGetDeleteIds = (deleteIds: string[]) => {
  deleteModal();
};
const handleCloseDialog = () => {
  console.log("关闭弹窗");
  resetForm(ruleFormRef.value);
};
const handleConfirmDialog = () => {
  console.log("确认弹窗");
  submitForm(ruleFormRef.value);
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      resetForm(formEl);
    } else {
      console.log("error submit!", fields);
    }
  });
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dialogViewVisible.value = false;
};
const handleSelctImage = type => {
  imageType.value = type;
  dialogImageSelectVisible.value = true;
};
const handleGetImage = (arr: any) => {
  shopTypeForm[imageType.value] = arr[0].url;
  dialogImageSelectVisible.value = false;
  imageUrl.value = "";
};
const handleCloseImageDialog = () => {
  dialogImageSelectVisible.value = false;
};
</script>
<template>
  <div class="shop-type-list-container">
    <el-image-viewer
      v-if="showViewer"
      :url-list="[imageUrl]"
      @close="closeViewer"
    />
    <ReTable
      class="content-body"
      type="shopTypeList"
      create-btn-text="添加商品分类"
      delete-btn-text="删除选中商品分类"
      @delete="handleGetDeleteIds"
      @create="handleGetCreate"
    />
    <ReNewDialog
      :visible="dialogViewVisible"
      :dialogTitle="dialogTitle"
      @close="handleCloseDialog"
      @confirm="handleConfirmDialog"
    >
      <template #content>
        <el-form
          ref="ruleFormRef"
          :model="shopTypeForm"
          :rules="rules"
          label-width="140px"
        >
          <el-form-item label="标题" prop="title">
            <el-input v-model="shopTypeForm.title" placeholder="请输入标题" />
          </el-form-item>
          <el-form-item label="图片" prop="image">
            <div>
              <div class="flex items-center">
                <ReUpload class="mr-3" />
                <el-button @click="handleSelctImage('image')">
                  选择图片
                </el-button>
                <ReImageDialog
                  :visible="dialogImageSelectVisible"
                  @confirm="handleGetImage"
                  @close="handleCloseImageDialog"
                />
              </div>
              <div v-if="shopTypeForm.image != ''" class="mt-3">
                <el-image
                  style="width: 100px; height: 100px; border-radius: 10px"
                  :src="shopTypeForm.image"
                  fit="fill"
                />
              </div>
            </div>
          </el-form-item>
          <el-form-item label="排序" prop="status">
            <el-input v-model="shopTypeForm.sort" placeholder="请输入排序" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="shopTypeForm.status">
              <el-radio value="1" size="large">启用</el-radio>
              <el-radio value="0" size="large">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </template>
    </ReNewDialog>
  </div>
</template>
